package com.zyh.system.supermark1.service.impl;

import com.zyh.system.supermark1.dao.impl.GoodsDaoImpl;
import com.zyh.system.supermark1.domain.BuyCar;
import com.zyh.system.supermark1.domain.Goods;
import com.zyh.system.supermark1.service.GoodsService;

import java.math.BigDecimal;
import java.util.Scanner;

public class GoodsServiceImpl implements GoodsService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final BuyCarServiceImpl CAR_SERVICE = new BuyCarServiceImpl();
    public static final GoodsDaoImpl GOODS_DAO = new GoodsDaoImpl();

    // 购买商品
    @Override
    public void shopping() {
        int sumNum = 0;
        outer:
        while (true) {
            GOODS_DAO.selectAllGoods();
            System.out.println("请输入商品编号：");
            String id = INPUT.next();
            Goods selectGoods = GOODS_DAO.selectByGoodsId(id);
            if (selectGoods != null) {
                //获取用户输入编号对应商品的库存
                if (selectGoods.getStock() == 0) {
                    System.out.println("库存为0，无法购买，等待补货");
                    continue;
                }
                while (true) {
                    System.out.println("请输入购买数量：");
                    Integer buyNum = INPUT.nextInt();
                    if (buyNum <= 0) {
                        System.out.println("购买数量不能为零或者负数");
                        continue;
                    }
                    if (buyNum > selectGoods.getStock()) {
                        System.out.println("购买数量必须小于" + selectGoods.getStock());
                        continue;
                    } else {
                        System.out.println("您还要继续购买吗？(是y 否任意键)");
                        INPUT.nextLine();
                        String choice = INPUT.nextLine();
                        switch (choice) {
                            case "y":
                                BuyCar buyCar = new BuyCar(selectGoods.getId(), selectGoods.getName(), selectGoods.getPrice(), buyNum);
                                CAR_SERVICE.add(buyCar);
                                int count = GOODS_DAO.updateGoodsStockInfo(buyCar.getId(), selectGoods.getStock() - buyNum);
                                if (count > 0) {
                                    System.out.println("库存修改成功");
                                }
                                continue outer;
                            default:
                                BuyCar buyCar1 = new BuyCar(selectGoods.getId(), selectGoods.getName(), selectGoods.getPrice(), buyNum);
                                CAR_SERVICE.add(buyCar1);
                                int count1 = GOODS_DAO.updateGoodsStockInfo(buyCar1.getId(), selectGoods.getStock() - buyNum);
                                if (count1 > 0) {
                                    System.out.println("库存修改成功");
                                }
                                return;
                        }
                    }
                }
            } else {
                System.out.println("您输入的商品编号不存在");
                continue outer;
            }
        }
    }

    // 展示所有商品
    @Override
    public void showAllGoods() {
        GOODS_DAO.selectAllGoods();
    }

    // 添加商品
    @Override
    public void addGoods() {
        System.out.println("请输入要添加的商品编号：");
        String addGoodsId = INPUT.next();
        Goods goods = GOODS_DAO.selectByGoodsId(addGoodsId);
        // 判断查询到的 goods 对象是否为空
        if (goods == null) {// 添加的商品编号不存在数据库中
            System.out.println("请输入商品名称：");
            String addGoodsName = INPUT.next();
            // 校验输入的价格
            double inputPrice = 0;
            BigDecimal addGoodsPrice = null;
            while (true) {
                System.out.println("请输入商品价格：");
                inputPrice = INPUT.nextDouble();
                if (inputPrice < 0) {
                    System.out.println("价格不能小于0");
                } else {
                    addGoodsPrice = new BigDecimal(inputPrice);
                    break;
                }
            }
            // 校验输入的库存
            int addGoodStock = 0;
            while (true) {
                System.out.println("请输入商品库存：");
                addGoodStock = INPUT.nextInt();
                if (addGoodStock < 0) {
                    System.out.println("库存不能小于不能小于0");
                } else {
                    break;
                }
            }
            // 封装成 Goods 对象
            Goods addGoods = new Goods(addGoodsId, addGoodsName, addGoodsPrice, addGoodStock);
            // 调用 DAO 方法
            int count = GOODS_DAO.addGoods(addGoods);
            if (count > 0) {
                System.out.println("添加商品信息成功");
            } else {
                System.out.println("添加商品信息失败");
            }
        } else {// 不为 null 就代表输入的编号已经存在数据库中，则进行商品信息修改
            System.out.println("您输入的商品编号已经存在，是否进行修改？y/n");
            String confirm = INPUT.next();
            if ("y".equals(confirm)) {

                System.out.println("请输入修改后的商品名称：");
                String updateGoodsName = INPUT.next();
                // 校验输入的价格
                double inputPrice = 0;
                BigDecimal updateGoodsPrice = null;
                while (true) {
                    System.out.println("请输入商品价格：");
                    inputPrice = INPUT.nextDouble();
                    if (inputPrice < 0) {
                        System.out.println("价格不能小于0");
                    } else {
                        updateGoodsPrice = new BigDecimal(inputPrice);
                        break;
                    }
                }
                // 校验输入的库存
                int updateGoodStock = 0;
                while (true) {
                    System.out.println("请输入商品库存：");
                    updateGoodStock = INPUT.nextInt();
                    if (updateGoodStock < 0) {
                        System.out.println("库存不能小于不能小于0");
                    } else {
                        break;
                    }
                }
                int count = GOODS_DAO.updateGoodsInfo(new Goods(addGoodsId, updateGoodsName, updateGoodsPrice, updateGoodStock));
                if (count > 0) {
                    System.out.println("修改商品信息成功");
                }
            }
        }
    }
}